package com.zj.healthmanage.controller;

/*
 * @author 张杰
 * @date 2022/2/25 0025 14:00
 * @description
 */

import com.zj.healthmanage.constant.MessageConstant;
import com.zj.healthmanage.entity.Result;
import com.zj.healthmanage.pojo.Ordersetting;
import com.zj.healthmanage.service.OrdersettingService;
import com.zj.healthmanage.utils.POIUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ordersetting")
@Api(value = "预约设置",description = "预约设置")
public class OrderSettingController {

    @Autowired
    private OrdersettingService ordersettingService;

    @ApiOperation(value = "文件上传",httpMethod = "POST")
    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile) throws ParseException {
        try {
            //读取Excel文件数据
            List<String[]> excelInfoList = POIUtils.readExcel(excelFile);
            if(excelInfoList != null && excelInfoList.size() > 0){
                List<Ordersetting> ordersettingList = new ArrayList<>();
                for (String[] excelInfo : excelInfoList) {
                    String orderDate = excelInfo[0];
                    String number = excelInfo[1];
                    Ordersetting orderSetting = new Ordersetting();
                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                    Date date = dateFormat.parse(orderDate);
                    orderSetting.setOrderdate(date);
                    orderSetting.setNumber(Integer.parseInt(number));
                    ordersettingList.add(orderSetting);
                }
                ordersettingService.addOrderSetting(ordersettingList);
            }
        } catch (IOException e) {
            e.printStackTrace();//文件解析失败
            return new Result(false, "批量导入预约设置数据失败");
        }
        return new Result(true,"批量导入预约设置数据成功");
    }

    @ApiOperation(value = "查询每月可预约人数",httpMethod = "POST")
    @RequestMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String date){//yyyy-MM
        try{
            List<Map> list = ordersettingService.getOrderSettingByMonth(date);
            return new Result(true,"获取预约设置数据成功",list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"获取预约设置数据失败");
        }
    }

    @ApiOperation(value = "编辑预约人数",httpMethod = "POST")
    @RequestMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody Ordersetting ordersetting) {
        try {
            ordersettingService.editNumberByDate(ordersetting);
            return new Result(true, "预约设置成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "预约设置成功");
        }
    }
}
